//=============================================================================
#include "CClients.h"

//=============================================================================
CClients CClients::Clients[MAX_CLIENTS];

//=============================================================================
CClients::CClients() {
    UID         = "";
    PlayerID    = 0;

	Socket      = 0;

	Mode        = CCLIENTS_MODE_LOGIN;

	LastUpdate  = 0;
}

//-----------------------------------------------------------------------------
void CClients::Connect(int Socket) {
    Files.clear();
    Packets.clear();

    UID  = "";
    Mode = CCLIENTS_MODE_LOGIN;

	this->Socket = Socket;

	//NLaddress ClientAddr;
	//nlGetRemoteAddr(PotentialClient, &ClientAddr);
}

//-----------------------------------------------------------------------------
void CClients::Disconnect() {
    UID  = "";
    Mode = CCLIENTS_MODE_LOGIN;

	NET2_TCPClose(Socket);

	Socket = 0;
}

//-----------------------------------------------------------------------------
bool CClients::IsAvailable() {
	return (Socket <= 0);
}

//=============================================================================
CNetworkPacket* CClients::GetReadPacket() {
    if(Packets.size() > 0) {
        CNetworkPacket* EndPacket = &Packets[Packets.size() - 1];

        if(EndPacket->IsReady() == false) {
            return EndPacket;
        }
    }

    CNetworkPacket NewPacket;

    Packets.push_back(NewPacket);

    return &Packets[Packets.size() - 1];
}

//-----------------------------------------------------------------------------
int CClients::GetSocket() {
	return Socket;
}

//=============================================================================
int CClients::GetMode() {
    return Mode;
}

//-----------------------------------------------------------------------------
void CClients::SetMode(int Mode) {
    this->Mode = Mode;
}

//=============================================================================
void CClients::ScanFiles(std::string Path) {
    WIN32_FIND_DATA FindFileData;

    std::string ScanPath = "./" + Path + "/*.*";

	HANDLE FindHandle = FindFirstFile(ScanPath.c_str(), &FindFileData);

	if(FindHandle != INVALID_HANDLE_VALUE) {
													//Skip over .
		FindNextFile(FindHandle, &FindFileData);	//Skip over ..

		while(FindNextFile(FindHandle, &FindFileData)) {
            if(strstr(FindFileData.cFileName, ".lua"))  continue;
            if(strstr(FindFileData.cFileName, ".db"))   continue;

		    CClientsFiles TempFile;

		    if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
		        ScanFiles(Path + "/" + FindFileData.cFileName);

                TempFile.Filename = "." + Path + "/" + FindFileData.cFileName;
                TempFile.Checksum = "0";
                TempFile.IsFolder = 1;
		    }else{
                TempFile.Filename = "." + Path + "/" + FindFileData.cFileName;
                TempFile.Checksum = "0";
                TempFile.IsFolder = 0;

                FILE* FileHandle = fopen(TempFile.Filename.c_str(), "rb");

                fseek(FileHandle, 0, SEEK_END);

                TempFile.TotalBytes = ftell(FileHandle);

                fclose(FileHandle);
		    }

			Files.push_back(TempFile);
		}

		FindClose(FindHandle);
	}
}

//=============================================================================
CClients* CClients::FindAvailable() {
	for(short i = 0;i < MAX_CLIENTS;i++) {
		if(Clients[i].IsAvailable()) {
			return &Clients[i];
		}
	}

	return NULL;
}

//-----------------------------------------------------------------------------
CClients* CClients::FindBySocket(int Socket) {
	for(short i = 0;i < MAX_CLIENTS;i++) {
		if(Clients[i].GetSocket() == Socket) {
			return &Clients[i];
		}
	}

	return NULL;
}

//=============================================================================
bool CClients::OnInit() {
	return true;
}

//=============================================================================
